//
//  CommissionSubVc.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/5.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "CommissionSubVc.h"
#import "CommissionCell.h"
#import "CommissionHeadCell.h"
#import "CommissionCell.h"
#import "MJRefresh.h"
#import "BrokerCommissionVc.h"
#import "StoreCommissionDetailVc.h"
#import "BdTV.h"
#import "config.h"
#import "Tools.h"
#import "TxtReadVc.h"





@interface CommissionSubVc ()<CommissionHeadCellDelegate>
@property (strong, nonatomic) DealerDataManage *dm;
@property(nonatomic,assign) int pageIndex;
@property (strong, nonatomic) NSArray *dataArray;
@property (strong, nonatomic) NSMutableDictionary *parmDic;

@end

@implementation CommissionSubVc
@synthesize dm,pageIndex,dataArray,parmDic;




- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    
    
    [self refreshData];
    
}



-(void)initView{
    [super initView];
    
    
    dm = [DealerDataManage new];
    
    UIView *hed = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 18)];
    [self.tableView setTableHeaderView:hed];
    

    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMore)];

    
}



-(void)queryDataByBirthDay:(BirthDay*)begin to:(BirthDay*)end withBrokerID:(NSString*)flag{

    
    self.tableIndex = Commission_tab0;
    
    parmDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d-%d-%d",begin.year,begin.month,begin.day],@"startTime",[NSString stringWithFormat:@"%d-%d-%d",end.year,end.month,end.day],@"endTime", nil];
    
    if (![StringTool isBlankString:flag]) {
        [parmDic setObject:flag forKey:@"brokerId"];
    }
    
    BdTV *title = [[BdTV alloc]initWithFrame:CGRectMake(0, 0, PMW, 44)];
    [title.backBtn setTag:11];
    [title.backBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [[title doneBtn] removeFromSuperview];
    [title setMianTitle:@"周期查询"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:title];
    
    
    UIButton *btn = [self getHeadQueryBtn:[NSString stringWithFormat:@"%d-%d-%d 至 %d-%d-%d",begin.year,begin.month,begin.day,end.year,end.month,end.day]];
    btn.tag = 11;
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)queryDataByBirthDay:(BirthDay*)begin to:(BirthDay*)end{
    
    [self queryDataByBirthDay:begin to:end withBrokerID:nil];
}





-(void)queryDataByPrimissId:(NSString*)primissId withHourseName:(NSString*)name withBrokerID:(NSString*)flag{


    self.tableIndex = Commission_tab0;
    
    parmDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:primissId,@"premisesId", nil];
    
    if (![StringTool isBlankString:flag]) {
        [parmDic setObject:flag forKey:@"brokerId"];
    }
    
    BdTV *title = [[BdTV alloc]initWithFrame:CGRectMake(0, 0, PMW, 44)];
    [title.backBtn setTag:11];
    [title.backBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [[title doneBtn] removeFromSuperview];
    [title setMianTitle:@"楼盘查询"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:title];
    
    
    UIButton *btn = [self getHeadQueryBtn:name];
    btn.tag = 11;
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];

}


-(void)queryDataByPrimissId:(NSString*)primissId withHourseName:(NSString*)name{
    
    [self queryDataByPrimissId:primissId withHourseName:name withBrokerID:nil];
}



-(void)queryDataByStoreCode:(NSString*)storeCode withStoreName:(NSString*)name{

    self.tableIndex = Commission_tab0;
    parmDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:storeCode,@"storeCode", nil];
    
    BdTV *title = [[BdTV alloc]initWithFrame:CGRectMake(0, 0, PMW, 44)];
    [title.backBtn setTag:11];
    [title.backBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [[title doneBtn] removeFromSuperview];
    [title setMianTitle:@"门店查询"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:title];
    
    
    UIButton *btn = [self getHeadQueryBtn:name];
    btn.tag = 11;
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    
}


-(void)queryDataByBrokerId:(NSString*)brokerId withBorkerName:(NSString*)name{

    self.tableIndex = Commission_tab0;

    parmDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:brokerId,@"brokerId", nil];
    
    BdTV *title = [[BdTV alloc]initWithFrame:CGRectMake(0, 0, PMW, 44)];
    [title.backBtn setTag:11];
    [title.backBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [[title doneBtn] removeFromSuperview];
    [title setMianTitle:@"经纪人查询"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:title];
    
    
    UIButton *btn = [self getHeadQueryBtn:name];
    btn.tag = 11;
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];


}



-(UIButton*)getHeadQueryBtn:(NSString*)title{
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, PMW, 42+15)];
    UIView *lineb = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(headView.frame), 1)];
    [lineb setBackgroundColor:UIColorFromRGB(0xe3e3e3)];
    [headView addSubview:lineb];
    
    UIView *lineE = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(headView.frame)-15-1, PMW, 1)];
    [lineE setBackgroundColor:UIColorFromRGB(0xe3e3e3)];
    [headView addSubview:lineE];
    
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[Tools getImgByColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setFrame:CGRectMake(0, GET_VIEW_BOTTOM_POS(lineb), PMW, 40)];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [btn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn.contentEdgeInsets = UIEdgeInsetsMake(0,35, 0, 0);
    [headView addSubview:btn];
    
    UIImageView *imgv = [UIImageView new];
    [imgv setFrame:CGRectMake(10, (CGRectGetHeight(headView.frame)-15)/2-15/2, 15, 15)];
    [imgv setImage:[UIImage imageNamed:@"search_icom"]];
    [headView addSubview:imgv];
    
    
    {
        UIImageView *imgv = [UIImageView new];
        [imgv setFrame:CGRectMake(CGRectGetWidth(headView.frame)-10-6,  (CGRectGetHeight(headView.frame)-15)/2-15/2, 6, 15)];
        [imgv setImage:[UIImage imageNamed:@"right_jiantou2"]];
        [headView addSubview:imgv];
    }
    
    
    [self.tableView setTableHeaderView:headView];
    
    return btn;
}





-(void)refreshData{
    [super refreshData];
    
    [self.nodataCell setState:nodataCellStateLoading];
    
    self.pageIndex = 1;
    __weak typeof(self) baseSelf = self;
    
    if(parmDic==nil){
        parmDic = [NSMutableDictionary new];
    }
    
    [dm refreshYongJinListDataPageIndex:pageIndex parm:parmDic tableIndex:self.tableIndex  CallBack:^(ResulteMod *resultMod) {
       
        [baseSelf.tableView.header endRefreshing];
        [baseSelf.nodataCell setState:nodataCellStateNorMal];
        
        if(resultMod.resltState==ResultNetError){
            
            [baseSelf.nodataCell setState:nodataCellStateNetError];
            
        }else if (resultMod.resltState==ResultFail){
            
            [baseSelf.nodataCell setCodeErrorTip:resultMod.resultCode];
            
        }else{
            
            baseSelf.dataArray = resultMod.dataMod;
            if (baseSelf.dataArray.count>0) {
                pageIndex++;
            }
        }
        
         [baseSelf.tableView reloadData];
    }];
}




-(void)loadMore{
    [super loadMore];
    
     __weak typeof(self) baseSelf = self;
    [dm refreshYongJinListDataPageIndex:pageIndex parm:parmDic tableIndex:self.tableIndex  CallBack:^(ResulteMod *resultMod) {
        
        [baseSelf.tableView.footer endRefreshing];
        
        if(resultMod.resltState==ResultNetError){
            
            
        }else if (resultMod.resltState==ResultFail){
            
            
        }else{
            
            NSArray *pageData = resultMod.dataMod;
            if (pageData.count>0) {
                pageIndex++;
                
                NSMutableArray *array = [NSMutableArray new];
                [array addObjectsFromArray:baseSelf.dataArray];
                [array addObjectsFromArray:pageData];
                baseSelf.dataArray = array;
            }
            
            [baseSelf.tableView reloadData];
        }
    }];
}





-(void)btnOnClick:(UIButton *)sender{
    
    if (sender==self.nodataCell.refreshBtn) {
        
        [self refreshData];
    }else if (sender.tag==11){
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)nameBtnOnClick:(UIButton *)sender{

    CommissionData *data = [dataArray objectAtIndex:sender.tag];
    
    StoreCommissionBroker *broker = [[StoreCommissionBroker alloc]init];
    broker.name = data.name;
    broker.brokerId = data.brokerId;
    
    BrokerCommissionVc *vc = [[BrokerCommissionVc alloc]init];
    vc.bufBroker = broker;
    
    if (self.nvc==nil) {
        self.nvc = self.navigationController;
    }
    [self.nvc pushViewController:vc animated:YES];
    
}


-(void)companyBtnOnClick:(UIButton *)sender{
    
     CommissionData *data = [dataArray objectAtIndex:sender.tag];
    
    StoreCommissionDetailVc *vc = [[StoreCommissionDetailVc alloc]init];
    
    StoreCommission *buf = [[StoreCommission alloc]init];
    buf.storeName = data.company;
    buf.storeCode = data.companyId;
    
    vc.bufData = buf;
    
    if (self.nvc==nil) {
        self.nvc = self.navigationController;
    }
    [self.nvc pushViewController:vc animated:YES];
    
}
    



#pragma mark tableView delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (dataArray.count==0) {
        return tableView.frame.size.height;
    }
    
    CommissionData *data = [dataArray objectAtIndex:indexPath.row];
    
    if (data.CELL_STYLE==CELL_STYLE_HEAD) {
        return [CommissionHeadCell getHight];
        
    }else{
        return [CommissionCell getHight:nil withTableView:tableView];
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (dataArray&&dataArray.count>0) {
        [self.tableView setScrollEnabled:YES];
        return self.dataArray.count;
    }else{
        [self.tableView setContentOffset:CGPointMake(0, 0)];
        [self.tableView setScrollEnabled:NO];
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (dataArray.count==0) {
        return self.nodataCell;
    }
    
    
    CommissionData *data = [dataArray objectAtIndex:indexPath.row];
    
    if (data.CELL_STYLE==CELL_STYLE_HEAD) {
    
         CommissionHeadCell *cell = [CommissionHeadCell creatViewWithTableView:self.tableView];
        cell.delegate = self;
        [cell setData:data];
        
        return cell;
        
    }else{
        
        static NSString *ID = @"CommissionCell";
        CommissionCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil)
        {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"CommissionCell" owner:nil options:nil]lastObject];
            
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            
            [[cell nameBtn]addTarget:self action:@selector(nameBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];

            [[cell companyBtn]addTarget:self action:@selector(companyBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        
        
        [[cell nameBtn]setTag:indexPath.row];
        [[cell companyBtn]setTag:indexPath.row];
        
        CommissionData *data = [dataArray objectAtIndex:indexPath.row];
        
        [cell setData:data];
        
        return cell;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dataArray.count>0) {
      
    }
}

-(void)CommissionHeaderCellQuestionBtnOnClick{
    
    TxtReadVc *vc = [TxtReadVc new];
    vc.titleStr = @"佣金明细";
    vc.subCategoryTitleStr = @"佣金何时到账 ?";
    vc.txtStr = @"客户付完尾款，收房一到两个工作日佣金到账。";
    
      vc.hidesBottomBarWhenPushed = YES;;
    [self.navigationController pushViewController:vc animated:YES];
}


@end
